unit groupselect;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, Grids,
  Buttons, StdCtrls;

type

  { TfrmGroupSelect }

  TfrmGroupSelect = class(TForm)
    btnClose: TBitBtn;
    btnSelect: TBitBtn;
    btnSelectToRoot: TBitBtn;
    StringGridGroups: TStringGrid;
    procedure btnSelectToRootClick(Sender: TObject);
    procedure btnSelectClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure checkErrors;
    procedure refreshList;
    procedure StringGridGroupsDblClick(Sender: TObject);
    procedure StringGridGroupsDrawCell(Sender: TObject; aCol, aRow: Integer;
      aRect: TRect; aState: TGridDrawState);
  private
    { private declarations }
  public
    { public declarations }
    groupsId:Integer;
    selectedGroupId:String;
    selectedGroupTitle:String;
  end;

var
  frmGroupSelect: TfrmGroupSelect;
  LastGroupRecordNumber:String;
implementation

{$R *.lfm}

{ TfrmGroupSelect }

uses main,nomenclature,ZAbstractRODataset;

procedure TfrmGroupSelect.checkErrors;
begin
  if (StringGridGroups.RowCount=1) then begin
    btnSelect.Enabled:=False;
  end else begin
    btnSelect.Enabled:=True;
  end;
end;

procedure TfrmGroupSelect.refreshList;
var
  i:Integer;
begin
  StringGridGroups.RowCount:=FrmMain.DSGroupSelect.DataSet.RecordCount+1;
  for i:=1 to FrmMain.DSGroupSelect.DataSet.RecordCount do begin
    FrmMain.DSGroupSelect.DataSet.RecNo:=i;
    StringGridGroups.Cells[1,i]:=FrmMain.DSGroupSelect.DataSet.FieldByName('id').AsString;
    StringGridGroups.Cells[2,i]:=FrmMain.DSGroupSelect.DataSet.FieldByName('title').AsString;
    StringGridGroups.Cells[3,i]:=FrmMain.DSGroupSelect.DataSet.FieldByName('parentid').AsString;
  end;
end;

procedure TfrmGroupSelect.StringGridGroupsDblClick(Sender: TObject);
var
  id,parentid:String;
  p : TPoint;
  thisCell : TPoint;
  Column, Row: Longint;
begin
  p:=StringGridGroups.ScreenToClient(Mouse.CursorPos);
  StringGridGroups.MouseToCell(p.x,p.y,Column, Row);
  if ((Row>=1) AND (Column>=1)) then begin
    id:=StringGridGroups.Cells[1,StringGridGroups.Row];
    parentid:=StringGridGroups.Cells[3,StringGridGroups.Row];
    FrmMain.ZQGroupSelect.Filtered:=false;
    if (id=LastGroupRecordNumber) then begin
      FrmMain.ZQGroupSelect.Filter:='id='+parentid+' or parentid='+parentid+' AND isgroup=true AND id<>'+IntToStr(frmNomenclature.CurrentGroupRecordNumber);
      LastGroupRecordNumber:=parentid;
    end else begin
    if (id<>parentid) then begin
      FrmMain.ZQGroupSelect.Filter:='id='+id+' or parentid='+id+' AND isgroup=true AND id<>'+IntToStr(frmNomenclature.CurrentGroupRecordNumber);
      LastGroupRecordNumber:=id;
    end;
    end;
    FrmMain.ZQGroupSelect.Filtered:=true;
    refreshList;
  end;
end;

procedure TfrmGroupSelect.StringGridGroupsDrawCell(Sender: TObject; aCol,
  aRow: Integer; aRect: TRect; aState: TGridDrawState);
var
bmp: TBitmap;
begin
  if ((aCol=1) AND (aRow > 0)) then begin
  //if (StringGridGroups.Columns.Items[n].Title.Caption='id')  then begin
    bmp := TBitmap.Create;
    if (StringGridGroups.Cells[aCol,aRow]=LastGroupRecordNumber) then begin
      FrmMain.ImageList2.GetBitmap(3,bmp);
    end else begin
      FrmMain.ImageList2.GetBitmap(0,bmp);
    end;
    StringGridGroups.Canvas.Draw(aRect.Left,aRect.Top,bmp);
    bmp.Free;
  end;
end;

procedure TfrmGroupSelect.FormShow(Sender: TObject);
var
  tableName:String;
begin
  selectedGroupId:=EmptyStr;
  selectedGroupTitle:=EmptyStr;
  FrmMain.ZQGroupSelect.Active:=False;
  case groupsId of
  1: tableName:='nomenclature';
  2: tableName:='counterparties';
  end;
  FrmMain.ZQGroupSelect.Active:=False;
  FrmMain.ZQGroupSelect.SQL.Text:='SELECT id,"isgroup",title,parentid FROM '+tableName;
  FrmMain.ZQGroupSelect.SortedFields:='id,title';
  FrmMain.ZQGroupSelect.SortType:=stAscending;
  FrmMain.ZQGroupSelect.Filter:='isgroup=true AND parentid=0 AND id<>'+IntToStr(frmNomenclature.CurrentGroupRecordNumber);
  FrmMain.ZQGroupSelect.Filtered:=True;
  FrmMain.ZQGroupSelect.Active:=True;
  FrmMain.ZQGroupSelect.Open;
  refreshList;
  StringGridGroups.AllowOutboundEvents:=False;
end;

procedure TfrmGroupSelect.btnSelectClick(Sender: TObject);
begin
  selectedGroupId:=StringGridGroups.Cells[1,StringGridGroups.Row];
  selectedGroupTitle:=StringGridGroups.Cells[2,StringGridGroups.Row];
  Close;
end;

procedure TfrmGroupSelect.btnSelectToRootClick(Sender: TObject);
begin
  selectedGroupId:='0';
  selectedGroupTitle:='';
  Close;
end;

end.

